package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Result<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String username = employee.getUsername();
        String password = employee.getPassword();
        return employeeService.login(request, username, password);
    }

    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        return employeeService.logout(request);
    }

    @PostMapping
    public Result<String> addEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("要添加的员工信息==>" + employee.toString());
        return employeeService.addOne(request, employee);
    }

    @GetMapping("/page")
    public Result<Page<Employee>> page(int page, int pageSize, String name) {
        return employeeService.getPage(name,page,pageSize);
    }

    @PutMapping
    public Result<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info("修改员工信息");
        return employeeService.update(request,employee);
    }

    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id){
        return employeeService.getById(id);
    }
}
